package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnMores;
import com.example.zhunongzonghe.entity.ZnMoresClassification;
import com.example.zhunongzonghe.service.IZnMoresService;
import com.example.zhunongzonghe.service.ZnMoresClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/znMoresClassification")
public class ZnMoresClassificationController {

    @Autowired
    private ZnMoresClassificationService classificationService;
    @Autowired
    private IZnMoresService znMoresService;

    @GetMapping("/SearchClassifyAll")
    public Result QueryAllSort(@RequestParam("currentPage") Integer currentPage,
                               @RequestParam("pageSize") Integer pageSize){
        Page<ZnMoresClassification> page = new Page<>(currentPage,pageSize);
        Page<ZnMoresClassification> classificationPage = classificationService.page(page,null);
        return new Result<>(true,"查询成功",200,classificationPage);
    }

    @PostMapping("/AddMoresClassify")
    public Result AddMoresClassify(@RequestBody ZnMoresClassification znMoresClassification){
        znMoresClassification.setCreateTime(LocalDateTime.now());
        znMoresClassification.setUpdateTime(LocalDateTime.now());
        classificationService.save(znMoresClassification);
        return new Result<>(true,"添加成功",200);
    }

    @DeleteMapping("/deleteMoresClassify/{id}")
    public Result deleteMoresClassify(@PathVariable Integer id){
        classificationService.removeById(id);
        LambdaQueryWrapper<ZnMores> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnMores::getClassifyId,id);
        List<ZnMores> znMores = znMoresService.list(queryWrapper);
        znMoresService.removeBatchByIds(znMores);
        return new Result<>(true,"删除成功",200);
    }
}
